package com.manor.one.http;

import com.orhanobut.logger.Logger;
import com.manor.common.CommonAppConfig;
import com.manor.common.bean.HMatchUserBean;
import com.manor.common.http.CommonHttpUtil;
import com.manor.common.http.HttpCallback;
import com.manor.common.http.HttpClient;
import com.manor.common.interfaces.CommonCallback;
import com.manor.common.utils.Gson2Util;
import com.manor.common.utils.MD5Util;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.ToastUtil;

/**
 * Created by cxf on 2019/3/21.
 */

public class OneHttpUtil {

    /**
     * 取消网络请求
     */
    public static void cancel(String tag) {
        HttpClient.getInstance().cancel(tag);
    }


    /**
     * 获取主播印象列表
     */
    public static void getAllImpress(String touid, HttpCallback callback) {
        HttpClient.getInstance().get("User.GetUserLabel", OneHttpConsts.GET_ALL_IMPRESS)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("touid", touid)
                .execute(callback);
    }

    /**
     * 给主播设置印象
     */
    public static void setImpress(String touid, String ImpressIDs, HttpCallback callback) {
        HttpClient.getInstance().post("User.setUserLabel", OneHttpConsts.SET_IMPRESS)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("touid", touid)
                .params("labels", ImpressIDs)
                .execute(callback);
    }


    /**
     * 获取直播间举报内容列表
     */
    public static void getLiveReportList(HttpCallback callback) {
        HttpClient.getInstance().post("Live.getReportClass", OneHttpConsts.GET_LIVE_REPORT_LIST)
                .execute(callback);
    }

    /**
     * 举报用户
     */
    public static void setReport(String touid, String content, HttpCallback callback) {
        HttpClient.getInstance().post("Live.setReport", OneHttpConsts.SET_REPORT)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("touid", touid)
                .params("content", content)
                .execute(callback);
    }


    /**
     * 观众跟主播连麦时，获取自己的流地址
     */
    public static void getLinkMicStream(HttpCallback callback) {
        HttpClient.getInstance().post("Linkmic.RequestLVBAddrForLinkMic", OneHttpConsts.GET_LINK_MIC_STREAM)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .execute(callback);
    }

    /**
     * 主播连麦成功后，要把这些信息提交给服务器
     *
     * @param touid    连麦用户ID
     * @param pull_url 连麦用户播流地址
     */
    public static void linkMicShowVideo(String touid, String pull_url) {
        HttpClient.getInstance().post("Live.showVideo", OneHttpConsts.LINK_MIC_SHOW_VIDEO)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("liveuid", CommonAppConfig.getInstance().getUid())
                .params("touid", touid)
                .params("pull_url", pull_url)
                .execute(new HttpCallback() {
                    @Override
                    public void onSuccess(int code, String msg, String info) {

                    }
                });
    }


    /**
     * 主播设置是否允许观众发起连麦
     */
    public static void setLinkMicEnable(boolean linkMicEnable, HttpCallback callback) {
        HttpClient.getInstance().post("Linkmic.setMic", OneHttpConsts.SET_LINK_MIC_ENABLE)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("ismic", linkMicEnable ? 1 : 0)
                .execute(callback);
    }


    /**
     * 观众检查主播是否允许连麦
     */
    public static void checkLinkMicEnable(String liveUid, HttpCallback callback) {
        HttpClient.getInstance().post("Linkmic.isMic", OneHttpConsts.CHECK_LINK_MIC_ENABLE)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("liveuid", liveUid)
                .execute(callback);
    }


    /**
     * 认证时获取形象标签列表
     */
    public static void getImpressList(HttpCallback callback) {
        HttpClient.getInstance().post("Auth.GetLabel", OneHttpConsts.GET_IMPRESS_LIST)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(callback);
    }



    /**
     * 主播向观众赴约，检测观众状态，同时获取自己的推拉流地址
     *
     * @param subscribeId 预约的 id
     * @param callback
     */
    public static void chatAncToAudStart(String subscribeId, HttpCallback callback) {
        String uid = CommonAppConfig.getInstance().getUid();
        String token = CommonAppConfig.getInstance().getToken();
        String sign = MD5Util.getMD5(StringUtil.contact("subscribeid=", subscribeId, "&token=", token, "&uid=", uid, "&", HttpClient.SALT));
        HttpClient.getInstance().post("Live.ToAppointment", OneHttpConsts.CHAT_ANC_TO_AUD_START)
                .params("uid", uid)
                .params("token", token)
                .params("subscribeid", subscribeId)
                .params("sign", sign)
                .execute(callback);
    }


    /**
     * 主播向观众发起通话邀请，检测观众状态，同时获取自己的推拉流地址
     */
    public static void chatAncToAudStart2(String toUid, String type, HttpCallback callback) {
        String uid = CommonAppConfig.getInstance().getUid();
        String token = CommonAppConfig.getInstance().getToken();
        String sign = MD5Util.getMD5(StringUtil.contact("token=", token, "&touid=", toUid, "&type=", String.valueOf(type), "&uid=", uid, "&", HttpClient.SALT));
        HttpClient.getInstance().post("Live.AnchorLaunch", OneHttpConsts.CHAT_ANC_TO_AUD_START_2)
                .params("uid", uid)
                .params("token", token)
                .params("touid", toUid)
                .params("sign", sign)
                .execute(callback);
    }

    /**
     * 观众挂断通话
     *
     * @param liveUid       主播的 id
     * @param chatSessionId 通话的会话id
     * @param hangType      挂断类型 0等待中挂断 1等待结束后主播无响应挂断 2通话中挂断
     */
    public static void chatAudienceHangUp(String liveUid, String chatSessionId, String hangType, HttpCallback callback) {
        String uid = CommonAppConfig.getInstance().getUid();
        String token = CommonAppConfig.getInstance().getToken();
        String sign = MD5Util.getMD5(StringUtil.contact("liveuid=", liveUid, "&showid=", chatSessionId, "&token=", token, "&uid=", uid, "&", HttpClient.SALT));
        HttpClient.getInstance().post("Live.UserHang", OneHttpConsts.CHAT_AUDIENCE_HANG_UP)
                .params("uid", uid)
                .params("token", token)
                .params("liveuid", liveUid)
                .params("showid", chatSessionId)
                .params("hangtype", hangType)
                .params("sign", sign)
                .execute(callback != null ? callback : CommonHttpUtil.NO_CALLBACK);

    }


    /**
     * 主播挂断通话
     *
     * @param touid         通话对象的 id
     * @param chatSessionId 通话的会话id
     */
    public static void chatAnchorHangUp(String touid, String chatSessionId, HttpCallback callback) {
        String uid = CommonAppConfig.getInstance().getUid();
        String token = CommonAppConfig.getInstance().getToken();
        String sign = MD5Util.getMD5(StringUtil.contact("showid=", chatSessionId, "&token=", token, "&touid=", touid, "&uid=", uid, "&", HttpClient.SALT));
        HttpClient.getInstance().post("Live.AnchorHang", OneHttpConsts.CHAT_ANCHOR_HANG_UP)
                .params("uid", uid)
                .params("token", token)
                .params("touid", touid)
                .params("showid", chatSessionId)
                .params("sign", sign)
                .execute(callback != null ? callback : CommonHttpUtil.NO_CALLBACK);

    }

    /**
     * 主播同意通话
     *
     * @param touid         通话对象的 id
     * @param chatSessionId 通话的会话id
     */
    public static void chatAnchorAccpet(String touid, String chatSessionId, HttpCallback callback) {
        String uid = CommonAppConfig.getInstance().getUid();
        String token = CommonAppConfig.getInstance().getToken();
        String sign = MD5Util.getMD5(StringUtil.contact("showid=", chatSessionId, "&token=", token, "&touid=", touid, "&uid=", uid, "&", HttpClient.SALT));
        HttpClient.getInstance().post("Live.AnchorAnswer", OneHttpConsts.CHAT_ANCHOR_ACCPET)
                .params("uid", uid)
                .params("token", token)
                .params("touid", touid)
                .params("showid", chatSessionId)
                .params("sign", sign)
                .execute(callback);

    }

    /**
     * 观众同意通话
     *
     * @param liveUid       主播的 id
     * @param chatSessionId 通话的会话id
     */
    public static void chatAudienceAccpet(String liveUid, String chatSessionId, HttpCallback callback) {
        String uid = CommonAppConfig.getInstance().getUid();
        String token = CommonAppConfig.getInstance().getToken();
        String sign = MD5Util.getMD5(StringUtil.contact("liveuid=", liveUid, "&showid=", chatSessionId, "&token=", token, "&uid=", uid, "&", HttpClient.SALT));
        HttpClient.getInstance().post("Live.UserAnswer", OneHttpConsts.CHAT_AUDIENCE_ACCPET)
                .params("uid", uid)
                .params("token", token)
                .params("liveuid", liveUid)
                .params("showid", chatSessionId)
                .params("sign", sign)
                .execute(callback);

    }


    /**
     * 通话结束时候获取评价标签列表
     */
    public static void getChatEvaList(String liveUid, HttpCallback callback) {
        HttpClient.getInstance().post("Label.GetEvaluate", OneHttpConsts.GET_CHAT_EVA_LIST)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("liveuid", liveUid)
                .execute(callback);
    }

    /**
     * 通话结束时观众对主播进行评价
     */
    public static void setChatEvaList(String liveUid, String evaIds) {
        HttpClient.getInstance().post("Label.SetEvaluate", OneHttpConsts.SET_CHAT_EVA_LIST)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("liveuid", liveUid)
                .params("evaluateids", evaIds)
                .execute(CommonHttpUtil.NO_CALLBACK);
    }

    /**
     * 获取某人的评价列表
     */
    public static void getUserEvaList(String liveUid, int p, HttpCallback callback) {
        HttpClient.getInstance().post("Label.GetEvaluateList", OneHttpConsts.GET_USER_EVA_LIST)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("liveuid", liveUid)
                .params("p", p)
                .execute(callback);
    }

    /**
     * 获取某人的评价列表
     */
    public static void getUserEvaCalc(String liveUid, int p, HttpCallback callback) {
        HttpClient.getInstance().post("Label.GetEvaluateCount", OneHttpConsts.GET_USER_EVA_CALC)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("liveuid", liveUid)
                .params("p", p)
                .execute(callback);
    }


    /**
     * 计时收费的时候，观众每隔一分钟请求这个接口进行扣费
     *
     * @param liveUid 主播的uid
     */
    public static void timeCharge(String liveUid, String sessionId, boolean isPair, HttpCallback callback) {
        Logger.d("timeCharge=="+"liveUid = [" + liveUid + "], sessionId = [" + sessionId + "], isPair = [" + isPair +
                "],");
        int type = isPair ? 1 : 0;
        HttpClient.getInstance().post("Live.TimeCharge", OneHttpConsts.TIME_CHARGE)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("liveuid", liveUid)
                .params("type",type)
                .params("showid", sessionId)
                .execute(callback);
    }


    /**
     * 我预约的主播列表
     */
    public static void getMySubscribeList(int p, HttpCallback callback) {
        HttpClient.getInstance().get("Subscribe.GetMeto", OneHttpConsts.GET_MY_SUBCRIBE_LIST)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("p", p)
                .execute(callback);
    }

    /**
     * 预约我的列表
     */
    public static void getSubscribeMeList(int p, HttpCallback callback) {
        HttpClient.getInstance().get("Subscribe.GetTome", OneHttpConsts.GET_SUBCRIBE_ME_LIST)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("p", p)
                .execute(callback);
    }

    /**
     * 获取预约的个数
     */
    public static void getSubscribeNums(HttpCallback callback) {
        HttpClient.getInstance().get("Subscribe.GetSubscribeNums", OneHttpConsts.GET_SUBSCRIBE_NUMS)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(callback);
    }


    /**
     * 观众在匹配前进行检测
     */
    public static void matchCheck(int type, HttpCallback callback) {
        HttpClient.getInstance().post("Match.Check", OneHttpConsts.MATCH_CHECK)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("type", type)
                .execute(callback);
    }


    /**
     * 获取匹配信息
     */
    public static void getMatchInfo(HttpCallback callback) {
        HttpClient.getInstance().post("Match.GetMatch", OneHttpConsts.GET_MATCH_INFO)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(callback);
    }


    /**
     * 主播匹配
     */
    public static void matchAnchor(int type, HttpCallback callback) {
        HttpClient.getInstance().post("Match.AnchorMatch", OneHttpConsts.MATCH_ANCHOR)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("type", type)
                .execute(callback);
    }


    /**
     * 主播取消匹配
     */
    public static void matchAnchorCancel() {
        HttpClient.getInstance().post("Match.AnchorCancel", OneHttpConsts.MATCH_ANCHOR_CANCEL)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(CommonHttpUtil.NO_CALLBACK);
    }


    /**
     * 用户匹配
     */
    public static void matchAudience(int type, HttpCallback callback) {
        HttpClient.getInstance().post("Match.UserMatch", OneHttpConsts.MATCH_AUDIENCE)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("type", type)
                .execute(callback);
    }

    public static void matchAudience(int type, CommonCallback<HMatchUserBean> callback) {
        HttpClient.getInstance().post("Match.UserMatch", OneHttpConsts.MATCH_AUDIENCE)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("type", type)
                .execute(new HttpCallback() {
                    @Override
                    public void onSuccess(int code, String msg, String info) {
                        if (code == 0 && StringUtil.isNotEmpty(info)) {
                            String str = info;
                            HMatchUserBean matchUserBean = Gson2Util.fromJson(str,HMatchUserBean.class);
                            if (callback!=null)
                                callback.callback(matchUserBean);
                        }else {
                            ToastUtil.show(msg);
                        }
                    }

                });
    }

    /**
     * 用户取消匹配
     */
    public static void matchAudienceCancel() {
        HttpClient.getInstance().post("Match.UserCancel", OneHttpConsts.MATCH_AUDIENCE_CANCEL)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(CommonHttpUtil.NO_CALLBACK);
    }



    /**
     * 获取在线用户匹配
     *
     * @param p
     * @param callback
     */
    public static void getOnlineUser(int p, HttpCallback callback) {
        HttpClient.getInstance().post("Match.GetOnlineUser", OneHttpConsts.MATCH_ONLINE_USER)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("p", p)
                .execute(callback);
    }

    /**
     * 用户匹配
     *
     * @param mType
     * @param callback
     */
    public static void getUserMatch(int mType, HttpCallback callback) {
        HttpClient.getInstance().get("Match.UserMatch", OneHttpConsts.USER_MATCH)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("type", mType)
                .execute(callback);
    }

    /**
     * 用户取消
     *
     * @param callback
     */
    public static void getUserCancel( HttpCallback callback) {
        HttpClient.getInstance().post("Match.UserCancel", OneHttpConsts.USER_CANCEL)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(callback);
    }


    /**
     * 主播匹配
     *
     * @param mType
     * @param callback
     */
    public static void getAnchorMatch(int mType, HttpCallback callback) {
        HttpClient.getInstance().post("Match.UserMatch", OneHttpConsts.ANCHOR_MATCH)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .params("type", mType)
                .execute(callback);
    }

        /**
     * 主播取消
     *
     * @param callback
     */
    public static void getAnchorCancel( HttpCallback callback) {
        HttpClient.getInstance().post("Match.AnchorCancel", OneHttpConsts.ANCHOR_CANCEL)
                .params("uid", CommonAppConfig.getInstance().getUid())
                .params("token", CommonAppConfig.getInstance().getToken())
                .execute(callback);
    }
}
